package co.fitstart.admin.web.controller.courseware;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.GroupTemplateService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.dietary.DietaryProgramTemplate;
import co.fitstart.entity.courseware.dietary.DietaryProgramType;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareType;
import co.fitstart.entity.courseware.fitnessgroup.DailyCoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;

/**
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/fitness-groups/{fitnessGroupId}/batch-courseware-templates")
@MenuSetting("menu-course")
public class BatchCoursewareTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(BatchCoursewareTemplateController.class);
    
    @Autowired
    private GroupTemplateService groupTemplateService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<DietaryProgramTemplate> dietaryProgramTemplateService;

    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param type
     * @return
     */
    @RequestMapping(value = "/{weeks}", method = RequestMethod.GET, params = "type=weekly")
    @MenuSetting("submenu-fitness-group")
    public ModelAndView weeklyIndex(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                    @PathVariable("weeks")int weeks,
                                    @RequestParam("type") String type,
                                    @RequestParam(value="day", required=false) Integer day){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/weekly-courseware-template-index");
        
        FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(fitnessGroupId);
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        BatchCoursewareTemplate batchCoursewareTemplate = groupTemplateService.loadBatchCoursewareTemplate(fitnessGroup, weeks, BatchCoursewareType.aliasOf(type));
        modelAndView.addObject("batchCoursewareTemplate", batchCoursewareTemplate);
        
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("type", type);
        modelAndView.addObject("day", day);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param type
     * @return
     */
    @RequestMapping(value = "/{weeks}", method = RequestMethod.GET, params = "type=course")
    @MenuSetting("submenu-fitness-group")
    public ModelAndView courseIndex(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                    @PathVariable("weeks")int weeks,
                                    @RequestParam("type") String type,
                                    @RequestParam("day") int day){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/course-courseware-template-index");
        
        FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(fitnessGroupId);
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        BatchCoursewareTemplate batchCoursewareTemplate = groupTemplateService.loadBatchCoursewareTemplate(fitnessGroup, weeks, BatchCoursewareType.aliasOf(type));
        modelAndView.addObject("batchCoursewareTemplate", batchCoursewareTemplate);
        
        DailyCoursewareTemplate dailyCoursewareTemplate = groupTemplateService.loadDailyCoursewareTemplate(batchCoursewareTemplate, day);
        modelAndView.addObject("dailyCoursewareTemplate", dailyCoursewareTemplate);
        
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("type", type);
        modelAndView.addObject("day", day);
        
        return modelAndView;
    }
    
    @RequestMapping(value = "/{weeks}", method = RequestMethod.GET, params = "type=specialty")
    @MenuSetting("submenu-fitness-group")
    public ModelAndView specialtyIndex(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                       @PathVariable("weeks")int weeks,
                                       @RequestParam("type") String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/specialty-courseware-template-index");
        
        FitnessGroup fitnessGroup = groupTemplateService.loadFitnessGroup(fitnessGroupId);
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        BatchCoursewareTemplate batchCoursewareTemplate = groupTemplateService.loadBatchCoursewareTemplate(fitnessGroup, weeks, BatchCoursewareType.aliasOf(type));
        modelAndView.addObject("batchCoursewareTemplate", batchCoursewareTemplate);
        
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param batchId
     * @param dietType
     * @return
     */
    @RequestMapping(value = "/{batchId}/diet", method = RequestMethod.GET)
    public ModelAndView editDiet(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                 @PathVariable("batchId") Long batchId,
                                 @RequestParam("dietType") String dietType) {
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/batch-courseware-template-diet");
        
        BatchCoursewareTemplate batchCoursewareTemplate = groupTemplateService.loadBatchCoursewareTemplate(batchId);
        modelAndView.addObject("dietaryProgramTemplate", batchCoursewareTemplate.getDiet(DietaryProgramType.aliasOf(dietType)));
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("dietType", dietType);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @return
     */
    @RequestMapping(value = "/{batchId}/diet", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDiet(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                           @PathVariable("batchId") Long batchId,
                                           @RequestParam("dietType") String dietType,
                                           @RequestParam("dietaryProgramTemplateId") Long dietaryProgramTemplateId){
        
        Result result = new Result();
            
        try{
            
            BatchCoursewareTemplate batchCoursewareTemplate = groupTemplateService.loadBatchCoursewareTemplate(batchId);
            DietaryProgramTemplate dietaryProgramTemplate = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, dietaryProgramTemplateId);
            
            groupTemplateService.editDiet(batchCoursewareTemplate, dietaryProgramTemplate, DietaryProgramType.aliasOf(dietType));
            
            result.success().message("修改饮食计划成功！");
            
        } catch (Exception e) {
            
            logger.error("onEditDiet - ", e);
            result.fail().message("修改饮食计划失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param batchId
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/{batchId}/dietary-templates/{templateId}/snacks", method = RequestMethod.GET)
    public ModelAndView createSnack(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                    @PathVariable("batchId") Long batchId,
                                    @PathVariable("templateId") Long templateId) {
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/batch-courseware-template-snack-create");
        
        DietaryProgramTemplate template = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, templateId);
        modelAndView.addObject("snacks", template.getSnacks());
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("batchId", batchId);
        modelAndView.addObject("templateId", templateId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @return
     */
    @RequestMapping(value = "/{batchId}/dietary-templates/{templateId}/snacks", method = RequestMethod.PATCH)
    public @ResponseBody String onCreateSnack(@PathVariable("fitnessGroupId") Long fitnessGroupId,
                                              @PathVariable("batchId") Long batchId,
                                              @PathVariable("templateId") Long templateId,
                                              @RequestParam("dietaryProgramTemplateId") Long dietaryProgramTemplateId){
        
        Result result = new Result();
            
        try{
            
            DietaryProgramTemplate template = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, templateId);
            
            groupTemplateService.createSnack(template, dietaryProgramTemplateId);
            
            result.success().message("修改饮食计划成功！");
            
        } catch (Exception e) {
            
            logger.error("onEditSnack - ", e);
            result.fail().message("修改饮食计划失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{weeks}/daily-courseware-templates/{dailyCoursewareTemplateId}/rest", method = RequestMethod.GET)
    public ModelAndView rest(@PathVariable("fitnessGroupId")Long fitnessGroupId, 
                             @PathVariable("weeks")int weeks,
                             @PathVariable("dailyCoursewareTemplateId")Long dailyCoursewareTemplateId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/batch-courseware-template/daily-courseware-template-rest");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        modelAndView.addObject("weeks", weeks);
        modelAndView.addObject("dailyCoursewareTemplateId", dailyCoursewareTemplateId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @param weeks
     * @param dailyCoursewareTemplateId
     * @return
     */
    @RequestMapping(value = "/{weeks}/daily-courseware-templates/{dailyCoursewareTemplateId}/rest", method = RequestMethod.PATCH)
    public @ResponseBody String onRest(@PathVariable("fitnessGroupId")Long fitnessGroupId, 
                                       @PathVariable("weeks")int weeks,
                                       @PathVariable("dailyCoursewareTemplateId")Long dailyCoursewareTemplateId){
        
        Result result = new Result();
        
        try{
            
            DailyCoursewareTemplate dailyCoursewareTemplate = groupTemplateService.loadDailyCoursewareTemplate(dailyCoursewareTemplateId);
            
            groupTemplateService.rest(dailyCoursewareTemplate);
            
            result.success().message("设为休息日成功！")
                            .data("url", "/fitness-groups/" + fitnessGroupId + "/batch-courseware-templates/" + weeks + "?type=weekly&day=" + dailyCoursewareTemplate.getSorting());
        }
        catch (Exception e) {
            
            logger.error("onRest - ", e);
            result.fail().message("设为休息日失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
}
